<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <TitleSearch :show-before="true">
          <template v-slot:before>
            <el-button type="primary" @click="addFn">添加经纪</el-button>
          </template>
          <template v-slot:after>
            <el-input v-model.trim="searchStr" placeholder="请输入关键字" /><el-button :disabled="disabled" icon="el-icon-search" @click="searchFn" />
          </template>
        </TitleSearch>
        <el-table
          ref="table"
          :data="tableData"
          border
          height="600"
          style="width: 100%"
        >
          <el-table-column prop="image" label="图片">
            <template v-slot="{row}">
              <el-image :src="row.image" style="width: 150px; height: 150px;" />
            </template>
          </el-table-column>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="email" label="邮箱" />
          <el-table-column prop="phone" label="电话" />
          <el-table-column prop="address" label="地址" />
          <el-table-column prop="desc" label="介绍" />
          <el-table-column prop="address" label="操作">
            <template v-slot="{row}">
              <el-button v-if="assignPerm('edit_agents')" type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button v-if="assignPerm('del_agents')" type="danger" size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--
            分页器
            current-page: 绑定当前的页码 -- 当前页码会有高亮效果
            page-sizes: 每页展示数量的选项
            page-size：当前每页展示多少条数据
            layout：是分页器布局
            size-change：表示每页展示的数量发生变化的时候触发的事件
            current-change：表示当前页码发生变化的时候触发的事件
           -->
        <el-pagination
          align="center"
          :current-page.sync="page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="pagesize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
      <!-- 新增弹窗 -->
      <add-agents ref="addAgents" :dialog-visible.sync="dialogVisible" @update="getAgentsList" />
    </div>
  </div>
</template>

<script>
import { getAgentsList, delAgents, getNumAgents } from '@/api/agents'
import addAgents from './components/add-agents.vue'
export default {
  components: { addAgents },
  data() {
    return {
      // 表格数据
      tableData: [],
      // 添加弹窗
      dialogVisible: false,
      // 每页展示的数量
      pagesize: 5,
      // 当前绑定的页码
      page: 1,
      total: 0,
      // 搜索关键字
      searchStr: '',
      // 禁用按钮
      disabled: false
    }
  },
  created() {
    // 获取经纪列表
    this.getAgentsList()
    // 获取经纪总数量
    this.getNumAgents()
  },
  methods: {
    // 搜索关键字
    async searchFn() {
      if (this.searchStr === '') {
        // return this.$message.info('请输入关键字！')
        this.getAgentsList()
      } else {
        // 调用接口 查找关键字
        const res = await getAgentsList({
          name_contains: this.searchStr
        })
        // console.log(90, res)
        this.tableData = res
        this.total = res.length
      }
    },
    // 删除经纪
    delFn(id) {
      this.$confirm('确认删除该经纪吗？', '删除', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning'
      }).then(async() => {
        await delAgents(id)
        this.$message.success('删除成功')
        // 当我们删除最后一条数据的时候，需要请求上一页的数据，这样才能获取到数据并渲染出来
        // 当前页不能是第一页
        if (this.tableData.length === 1 && this.page > 1) {
          this.page--
        }
        this.getAgentsList()
      }).catch(() => {
        this.$message.info('取消删除')
      })
    },
    // 编辑经纪
    editFn(id) {
      this.dialogVisible = true
      // console.log(56, id)
      this.$refs.addAgents.getAgentsDetail(id)
    },
    // 添加经纪
    addFn() {
      this.dialogVisible = true
    },
    // 获取经纪列表
    async getAgentsList() {
      const res = await getAgentsList({
        _limit: this.pagesize,
        _start: this.pagesize * (this.page - 1),
        name_contains: ''
      })
      // console.log(40, res)
      this.tableData = res
      // 获取total数量
      this.getNumAgents()
      // 表格列过多，动态渲染列滚动条无法显示，用doLayout函数来重新触发刷新该表格
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    handleSizeChange(size) {
      // size:表示用户点击选项的值(每页展示的数据)
      this.pagesize = size
      this.getAgentsList()
    },
    handleCurrentChange(page) {
      // page表示当前页码数
      this.page = page
      this.getAgentsList()
    },
    // 获取经纪数量
    async getNumAgents() {
      const res = await getNumAgents()
      // console.log(124, res)
      this.total = res
    }
  }
}
</script>

<style lang="scss" >
.el-table{
  border-bottom: 1px solid #ddd;
  margin-bottom: 20px;
  padding-bottom:20px;
}
.el-table td div{
  text-align: center;
}
</style>
